OpenRoads Designer CONNECT Edition SDK Help

Annotate elevation for all triangle points and triangle center

The below code shows how to get the terrain triangles and add annotation to all terrain points and centroid of terrain triangles.

//Required References
using System;
using System.Collections;
using System.Diagnostics;
using Bentley.CifNET.GeometryModel.SDK;
using Bentley.CifNET.SDK;
using Bentley.DgnPlatformNET;
using Bentley.GeometryNET;
using Bentley.MstnPlatformNET;
using Bentley.TerrainModelNET;

  public void AnnotateEleForAllTrianglePointAndCenter()
        {
            try
            {
                Bentley.CifNET.GeometryModel.SDK.TerrainSurface terrainSurface = null;

                //Get active DGN file
                Bentley.DgnPlatformNET.DgnFile dgnFile = Session.Instance.GetActiveDgnFile();
                //Get active dgn model
                Bentley.DgnPlatformNET.DgnModel dgnModel = Session.Instance.GetActiveDgnModel();
                //Create connection to dgnModel
                Bentley.CifNET.SDK.ConsensusConnection consensusConnection = new ConsensusConnection(dgnModel);

                //Get or create text style
                DgnTextStyle textStyle = DgnTextStyle.GetByName("Line Length Label", dgnFile);

                if (null == textStyle)
                {
                    textStyle = new DgnTextStyle("Line Length Label", dgnFile);
                    textStyle.SetProperty(TextStyleProperty.Width, 400D);
                    textStyle.SetProperty(TextStyleProperty.Height, 400D);
                    textStyle.Add(dgnFile);
                }

                //Get active geometric model
                Bentley.CifNET.GeometryModel.SDK.GeometricModel geometricModel = consensusConnection.GetActiveGeometricModel();
                if (geometricModel.ActiveSurface == null) return;

                //Get active terrain
                terrainSurface = geometricModel.ActiveSurface as TerrainSurface;
                if (terrainSurface == null) return;

                ArrayList triangles = new ArrayList();

                //Browse terrain triangles
                bool processPointsFeatures(DTMDynamicFeatureInfo featureInfo, object oArg)
                {
                    if (oArg != null)
                    {
                        ArrayList al = oArg as ArrayList;
                        Bentley.CifNET.LinearGeometry.ClosedLineString cls = new Bentley.CifNET.LinearGeometry.ClosedLineString(featureInfo.FeaturePoints);
                        al.Add(cls);
                    }
                    return true;
                }

                DynamicFeaturesBrowsingDelegate hdl2P = new DynamicFeaturesBrowsingDelegate(processPointsFeatures);
                ArrayList tTriangle = new ArrayList();

                TrianglesBrowsingCriteria criteria = new TrianglesBrowsingCriteria();
                terrainSurface.DTM.BrowseTriangles(criteria, hdl2P, tTriangle);

                Bentley.GeometryNET.DPoint3d[] tPoints = new Bentley.GeometryNET.DPoint3d[3];
                foreach (Bentley.CifNET.LinearGeometry.ClosedLineString cls in tTriangle)
                {
                    /// Get vertices
                    tPoints = cls.GetVertices();

                    DPoint3d point1 = new DPoint3d(tPoints[0]);
                    DPoint3d point2 = new DPoint3d(tPoints[1]);
                    DPoint3d point3 = new DPoint3d(tPoints[2]);

                    //Calculate elevation for triangle points and centroid
                    string point1Elevation = point1.Z.ToString("N2");
                    string point2Elevation = point2.Z.ToString("N2");
                    string point3Elevation = point3.Z.ToString("N2");

                    double centroidZ = (point1.Z + point2.Z + point3.Z) / 3;
                    double elevation = centroidZ * 3.28084;
                    string centerElevation = elevation.ToString("N2");

                    //Create and add annotation to model
                    TextBlock textBlockPt1 = new TextBlock(textStyle, dgnModel);
                    textBlockPt1.AppendText(point1Elevation);
                    textBlockPt1.SetUserOrigin(point1);
                    Bentley.DgnPlatformNET.Elements.TextElement textElementPt1 = (Bentley.DgnPlatformNET.Elements.TextElement)Bentley.DgnPlatformNET.Elements.TextElement.CreateElement(null, textBlockPt1);
                    textElementPt1.AddToModel();

                    TextBlock textBlockPt2 = new TextBlock(textStyle, dgnModel);
                    textBlockPt2.AppendText(point2Elevation);
                    textBlockPt2.SetUserOrigin(point2);
                    Bentley.DgnPlatformNET.Elements.TextElement textElementPt2 = (Bentley.DgnPlatformNET.Elements.TextElement)Bentley.DgnPlatformNET.Elements.TextElement.CreateElement(null, textBlockPt2);
                    textElementPt2.AddToModel();

                    TextBlock textBlockPt3 = new TextBlock(textStyle, dgnModel);
                    textBlockPt3.AppendText(point3Elevation);
                    textBlockPt3.SetUserOrigin(point3);
                    Bentley.DgnPlatformNET.Elements.TextElement textElementPt3 = (Bentley.DgnPlatformNET.Elements.TextElement)Bentley.DgnPlatformNET.Elements.TextElement.CreateElement(null, textBlockPt3);
                    textElementPt3.AddToModel();

                    DPoint3d centroid = new DPoint3d((point1.X + point2.X + point3.X) / 3, (point1.Y + point2.Y + point3.Y) / 3, (point1.Z + point2.Z + point3.Z) / 3);
                    TextBlock textBlockCtr = new TextBlock(textStyle, dgnModel);
                    textBlockCtr.AppendText(centerElevation);
                    textBlockCtr.SetUserOrigin(centroid);
                    Bentley.DgnPlatformNET.Elements.TextElement textElementCtr = (Bentley.DgnPlatformNET.Elements.TextElement)Bentley.DgnPlatformNET.Elements.TextElement.CreateElement(null, textBlockCtr);
                    textElementCtr.AddToModel();
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }